<template>
    <div class="loginForm">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">
            <h3>超市管理系统</h3>
            <el-form-item label="账号：" prop="username">
                <el-input v-model="loginForm.username"></el-input>
            </el-form-item>

            <el-form-item label="密码：" prop="password">
                <el-input type="password" v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button style="background-color: #f5f2c6" @click="submitForm('loginForm')">登录</el-button>
                <el-button @click="resetForm('loginForm')">重置</el-button>
            </el-form-item>
        </el-form>

    </div>

</template>
<style>
    .loginForm {
        background-color: #a3f6f5;
        width: 400px;
        height: 250px;
        margin: 200px auto;
        text-align: center;
        padding-top: 10px;
        border-color: #555555;
        border-style: solid;
        border-width: 2px;
        border-radius: 15px;
        box-shadow: 10px 10px 5px #403c3c;
    }

    .loginForm input {
        width: 260px;

    }

    h3 {
        margin: 10px 0;
    }

    .loginForm button {
        margin-right: 40px;
    }
</style>
<script>
    import {ajaxPost, popup} from "@/assets/js/common";
    import Cookies from 'js-cookie'

    export default {
        data() {
            return {
                loginForm: {
                    username: "19881653720",
                    password: "123456"
                },
                rules: {
                    username: [
                        {required: true, message: '账号不能为空', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'},
                        {min: 6, max: 8, message: '密码长度为5-8位', trigger: 'blur'}
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        ajaxPost("/login", this.loginForm).then(res => {
                            res = res.data
                            if (res.code == 200) {
                                Cookies.set("token", res.data.token, {expires: 1/48})
                                Cookies.set("employee", JSON.stringify(res.data.employee), {expires: 1/48})
                                popup("登录成功，请稍等...")
                                this.$router.push("/index")
                            } else {
                                popup(res.msg, "warning")
                            }

                        });
                    } else {
                        popup("账号或密码格式不正确！", "error")
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>